package com.lfg.leetcode.leetcode0;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class No13RomanToInt {

    private static Map<Character, Integer> data = new HashMap<Character, Integer>();

    static {
        data.put('M', 1000);
        data.put('D', 500);
        data.put('C', 100);
        data.put('L', 50);
        data.put('X', 10);
        data.put('V', 5);
        data.put('I', 1);
    }


    public static int romanToInt(String s) {
        int last = 0;
        int ret = 0;
        int length = s.length();
        for (int i = 0; i < length; i++) {
            int d = data.get(s.charAt(i));
            if (last > 0) {
                if (d > last) {
                    ret -= last;
                } else {
                    ret += last;
                }
            }
            last = d;
        }
        ret += last;
        return ret;
    }

    public static void main(String[] args) {
        System.out.println(romanToInt("III"));
    }
}
